package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.GoodsclassOS;
import zozopo.warehouse.operations.GoodsclassTB;
import zozopo.warehouse.operations.GoodsclasslistOS;
import zozopo.warehouse.operations.GoodsclasslistTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class QueryGoodsAction extends ActionSupport implements Action,SessionAware,ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.GOODS.QUERY";
	
	//传入参数
	private String searchStr = "";
	private int goodsclassid = 0;
	private int day = -30;
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	//传出参数
	private List<GoodsTB> goodsList;
	Map<Integer,List<PurchaseTB>> warehouseInfoMapList;
	private List<GoodsclassTB> goodsclassList;
	private LinkedHashMap<Integer,GoodsclassTB> goodsclassMap;
	private  LinkedHashMap<Integer,GoodsclassTB> goodsclassMapForGoodsclassid;
	private Map<Integer,List<GoodsclasslistTB>> goodsclasslistInfoMapList;
	private Map<Integer,ReceiptTB> goodsSalesReportsMap;
	
	private Map<Object, Object> session;
	private HttpServletRequest request;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		GoodsOS goodsOS = new GoodsOS(); 
		PurchaseOS purchaseOS = new PurchaseOS(goodsOS.getConn());
		//UserOS userOS = new UserOS();
		
		GoodsclassOS goodsclassOS = new GoodsclassOS(goodsOS.getConn());
		GoodsclasslistOS  goodsclasslistOS = new GoodsclasslistOS(goodsOS.getConn());
		
		ReceiptOS receiptOS = new ReceiptOS(goodsOS.getConn());
		
		goodsclassMap = new  LinkedHashMap<Integer,GoodsclassTB>();
		goodsclassList = goodsclassOS.select();
		
		
		if(goodsclassid == 0){
			if(searchStr.equals("")){
				goodsList = goodsOS.selectGoodsOrderByName(beginPageNum, pageSize);
				rowNum = goodsOS.getRowNum();
			}else{
				goodsList = goodsOS.selectBySearchStr(searchStr,beginPageNum, pageSize);
				rowNum = goodsOS.getRowNumForSearchStr(searchStr);
			}
		}else{
			goodsclassMapForGoodsclassid = new  LinkedHashMap<Integer,GoodsclassTB>();
			getGoodsclassMapForId(goodsclassid);
			boolean isFrist = true;
			String goodsclassidStr = "";
			goodsclassidStr += "(";
			for(GoodsclassTB goodsclass:goodsclassMapForGoodsclassid.values()){
				if(isFrist){
					goodsclassidStr += goodsclass.getId_goodsclass();
					isFrist = false;
				}else{
					
					goodsclassidStr += "," + goodsclass.getId_goodsclass();
				}
				
			}
			goodsclassidStr += ")";
			
			
			
			goodsList = goodsOS.selectByGoodsclassidStr(goodsclassidStr,beginPageNum, pageSize);
			rowNum = goodsOS.getRowNumForGoodsclassidStr(goodsclassidStr);
			
		}
		
		warehouseInfoMapList = purchaseOS.getWarehouseInfoMapList(goodsList);
		
		goodsclasslistInfoMapList = goodsclasslistOS.getGoodsclassInfoMapList(goodsList);
		goodsSalesReportsMap = receiptOS.selectSalesReportsMapByGoodsList(goodsList, day);
		goodsclassMap = getGoodsclassMap(0);
		goodsOS.releaseConnection();
		
		return SUCCESS;
		
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有产品查询权限.");
			return;
		}
		
		
	}
	
	public LinkedHashMap<Integer,GoodsclassTB> getGoodsclassMap(int parentid){
		
		
		for(GoodsclassTB goodsclass:goodsclassList){
			if(!goodsclassMap.containsKey(goodsclass.getId_goodsclass())){//如果已不存在
				if(goodsclass.getParentid_goodsclass() == parentid){//如果父ID正常
					goodsclassMap.put(goodsclass.getId_goodsclass(), goodsclass); 
					getGoodsclassMap(goodsclass.getId_goodsclass());
				}
			}
		}
		
		return goodsclassMap;
	}
	
	public LinkedHashMap<Integer,GoodsclassTB>  getGoodsclassMapForId(int id){
		
		
		for(GoodsclassTB goodsclass:goodsclassList){
			if(!goodsclassMapForGoodsclassid.containsKey(goodsclass.getId_goodsclass())){//如果已不存在
				if(goodsclass.getParentid_goodsclass() == id || goodsclass.getId_goodsclass() == id){//如果父ID正常
					goodsclassMapForGoodsclassid.put(goodsclass.getId_goodsclass(), goodsclass); 
					getGoodsclassMapForId(goodsclass.getId_goodsclass());
				}
			}
		}
		
		return goodsclassMapForGoodsclassid;
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		
	}
	
	
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	public List<GoodsTB> getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List<GoodsTB> goodsList) {
		this.goodsList = goodsList;
	}
	public Map<Integer, List<PurchaseTB>> getWarehouseInfoMapList() {
		return warehouseInfoMapList;
	}
	public void setWarehouseInfoMapList(
			Map<Integer, List<PurchaseTB>> warehouseInfoMapList) {
		this.warehouseInfoMapList = warehouseInfoMapList;
	}
	public static String getLimitedcode() {
		return limitedCode;
	}
	public int getGoodsclassid() {
		return goodsclassid;
	}
	public void setGoodsclassid(int goodsclassid) {
		this.goodsclassid = goodsclassid;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public List<GoodsclassTB> getGoodsclassList() {
		return goodsclassList;
	}
	public void setGoodsclassList(List<GoodsclassTB> goodsclassList) {
		this.goodsclassList = goodsclassList;
	}
	public LinkedHashMap<Integer, GoodsclassTB> getGoodsclassMap() {
		return goodsclassMap;
	}
	public void setGoodsclassMap(LinkedHashMap<Integer, GoodsclassTB> goodsclassMap) {
		this.goodsclassMap = goodsclassMap;
	}
	public LinkedHashMap<Integer, GoodsclassTB> getGoodsclassMapForGoodsclassid() {
		return goodsclassMapForGoodsclassid;
	}
	public void setGoodsclassMapForGoodsclassid(
			LinkedHashMap<Integer, GoodsclassTB> goodsclassMapForGoodsclassid) {
		this.goodsclassMapForGoodsclassid = goodsclassMapForGoodsclassid;
	}
	public Map<Integer, List<GoodsclasslistTB>> getGoodsclasslistInfoMapList() {
		return goodsclasslistInfoMapList;
	}
	public void setGoodsclasslistInfoMapList(
			Map<Integer, List<GoodsclasslistTB>> goodsclasslistInfoMapList) {
		this.goodsclasslistInfoMapList = goodsclasslistInfoMapList;
	}
	public Map<Integer, ReceiptTB> getGoodsSalesReportsMap() {
		return goodsSalesReportsMap;
	}
	public void setGoodsSalesReportsMap(Map<Integer, ReceiptTB> goodsSalesReportsMap) {
		this.goodsSalesReportsMap = goodsSalesReportsMap;
	}

	
}
